package com.ruoyi.quanzi.controller;

import java.util.List;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.quanzi.domain.SunUser;
import com.ruoyi.quanzi.service.ISunUserService;

/**
 * 用户管理Controller
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/quanzi/user")
public class SunUserController extends BaseController
{
    @Autowired
    private ISunUserService sunUserService;

    /**
     * 查询用户列表
     */
    @PreAuthorize("@ss.hasPermi('quanzi:user:list')")
    @GetMapping("/list")
    public TableDataInfo list(SunUser sunUser)
    {
        startPage();
        List<SunUser> list = sunUserService.selectUserList(sunUser);
        return getDataTable(list);
    }

    /**
     * 获取用户详细信息
     */
    @PreAuthorize("@ss.hasPermi('quanzi:user:query')")
    @GetMapping(value = "/{uid}")
    public AjaxResult getInfo(@PathVariable("uid") Integer uid)
    {
        return success(sunUserService.selectUserById(uid));
    }

    /**
     * 根据用户名查询用户
     */
    @GetMapping("/username/{username}")
    public AjaxResult getByUsername(@PathVariable("username") String username)
    {
        return success(sunUserService.selectSunUserByUsername(username));
    }

    /**
     * 根据手机号查询用户
     */
    @GetMapping("/mobile/{mobile}")
    public AjaxResult getByMobile(@PathVariable("mobile") String mobile)
    {
        return success(sunUserService.selectSunUserByMobile(mobile));
    }

    /**
     * 根据openid查询用户
     */
    @GetMapping("/openid/{openid}")
    public AjaxResult getByOpenid(@PathVariable("openid") String openid)
    {
        return success(sunUserService.selectSunUserByOpenid(openid));
    }

    /**
     * 搜索用户
     */
    @GetMapping("/search")
    public AjaxResult searchUsers(@RequestParam("keyword") String keyword, 
                                 @RequestParam(value = "limit", defaultValue = "10") Integer limit)
    {
        List<SunUser> list = sunUserService.searchUsers(keyword, limit);
        return success(list);
    }

    /**
     * 校验用户名是否唯一
     */
    @GetMapping("/checkUsernameUnique")
    public AjaxResult checkUsernameUnique(@RequestParam("username") String username)
    {
        return success(sunUserService.checkUsernameUnique(username));
    }

    /**
     * 校验手机号是否唯一
     */
    @GetMapping("/checkMobileUnique")
    public AjaxResult checkMobileUnique(@RequestParam("mobile") String mobile)
    {
        return success(sunUserService.checkMobileUnique(mobile));
    }

    /**
     * 新增用户
     */
    @PreAuthorize("@ss.hasPermi('quanzi:user:add')")
    @Log(title = "用户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SunUser sunUser)
    {
        return toAjax(sunUserService.insertUser(sunUser));
    }

    /**
     * 修改用户
     */
    @PreAuthorize("@ss.hasPermi('quanzi:user:edit')")
    @Log(title = "用户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SunUser sunUser)
    {
        return toAjax(sunUserService.updateUser(sunUser));
    }

    /**
     * 更新用户登录信息
     */
    @PutMapping("/loginInfo")
    public AjaxResult updateLoginInfo(@RequestParam("uid") Integer uid, 
                                     @RequestParam("lastLoginIp") String lastLoginIp)
    {
        return toAjax(sunUserService.updateUserLoginInfo(uid, lastLoginIp));
    }

    /**
     * 删除用户
     */
    @PreAuthorize("@ss.hasPermi('quanzi:user:remove')")
    @Log(title = "用户", businessType = BusinessType.DELETE)
    @DeleteMapping("/{uids}")
    public AjaxResult remove(@PathVariable Integer[] uids)
    {
        return toAjax(sunUserService.deleteUserByIds(uids));
    }

    /**
     * 重置用户密码
     */
    @PreAuthorize("@ss.hasPermi('quanzi:user:resetPwd')")
    @Log(title = "用户", businessType = BusinessType.UPDATE)
    @PutMapping("/resetPwd")
    public AjaxResult resetPwd(@RequestBody SunUser sunUser)
    {
        return toAjax(sunUserService.resetUserPwd(sunUser));
    }

    /**
     * 获取用户统计信息
     */
    @GetMapping("/statistics")
    public AjaxResult getUserStatistics()
    {
        return success(sunUserService.getUserStatistics());
    }

    /**
     * 获取用户粉丝列表
     */
    @GetMapping("/fans/{uid}")
    public TableDataInfo getUserFans(@PathVariable("uid") Integer uid)
    {
        startPage();
        List<SunUser> list = sunUserService.selectUserFans(uid);
        return getDataTable(list);
    }

    /**
     * 获取用户关注列表
     */
    @GetMapping("/follows/{uid}")
    public TableDataInfo getUserFollows(@PathVariable("uid") Integer uid)
    {
        startPage();
        List<SunUser> list = sunUserService.selectUserFollows(uid);
        return getDataTable(list);
    }

    /**
     * 关注用户
     */
    @PostMapping("/follow/{uid}")
    public AjaxResult followUser(@PathVariable("uid") Integer uid)
    {
        return toAjax(sunUserService.followUser(uid));
    }

    /**
     * 取消关注用户
     */
    @DeleteMapping("/follow/{uid}")
    public AjaxResult unfollowUser(@PathVariable("uid") Integer uid)
    {
        return toAjax(sunUserService.unfollowUser(uid));
    }

    /**
     * 简单查询用户列表（测试用）
     */
    @GetMapping("/test/simple")
    public AjaxResult simple()
    {
        try {
            List<SunUser> list = sunUserService.selectUserList(new SunUser());
            return success(list);
        } catch (Exception e) {
            return error("查询失败: " + e.getMessage());
        }
    }
}